package com.xy.biz.mc.manager.impl;

import com.xy.biz.mc.dao.UserConnectionDao;
import com.xy.biz.mc.domain.bo.UserConnectionBO;
import com.xy.biz.mc.domain.converter.UserConnectionConverter;
import com.xy.biz.mc.domain.po.UserConnectionPO;
import com.xy.biz.mc.domain.query.UserConnectionQuery;
import com.xy.biz.mc.manager.UserConnectionManager;
import com.xy.lang.domain.KeyValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserConnectionManagerImpl implements UserConnectionManager {

    @Autowired
    private UserConnectionDao userConnectionDao;

    @Override
    public void save(UserConnectionBO userConnectionBO) {
        UserConnectionPO userConnectionPO = UserConnectionConverter.toPO(userConnectionBO);
        if(userConnectionDao.isUserConnectionExists(userConnectionPO.getUserId())) {
            userConnectionDao.updateUserConnection(userConnectionPO);
        } else {
            try {
                userConnectionDao.saveUserConnection(userConnectionPO);
            } catch (DuplicateKeyException e) {
                userConnectionDao.updateUserConnection(userConnectionPO);
            }
        }
    }

    @Override
    public void update(UserConnectionBO userConnectionBO) {
        userConnectionDao.updateUserConnection(UserConnectionConverter.toPO(userConnectionBO));
    }

    @Override
    public Long getServerId(Long userId) {
        return userConnectionDao.getServerId(userId);
    }

    @Override
    public Map<Long, Long> listServerId(Collection<Long> userIdCollection) {
        Map<Long, Long> serviceIdMap;
        List<KeyValue<Long, Long>> keyValueList = userConnectionDao.listServerId(userIdCollection);
        Map<Long, Long> foundMap = keyValueList == null ? new HashMap<>() : keyValueList.stream().collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue));
        if(userIdCollection.size() == foundMap.size()) {
            serviceIdMap = foundMap;
        } else {
            serviceIdMap = new HashMap<>();
            for(Long userId : userIdCollection) {
                serviceIdMap.put(userId, foundMap.get(userId));
            }
        }
        return serviceIdMap;
    }

    @Override
    public Map<Long, Long> listServerIdByUserGroupId(Long userGroupId) {
        List<KeyValue<Long, Long>> keyValueList = userConnectionDao.listServerIdByUserGroupId(userGroupId);
        return keyValueList == null ? new HashMap<>() : keyValueList.stream().collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue));
    }

    @Override
    public List<UserConnectionBO> listUserConnection(UserConnectionQuery query) {
        return UserConnectionConverter.fromPO(userConnectionDao.listUserConnection(query));
    }

    @Override
    public int countUserConnection(UserConnectionQuery query) {
        return userConnectionDao.countUserConnection(query);
    }

}
